<?php
namespace Admin\Controller;
use Think\Controller;
class PrizeController extends BaseController {
   public function prizeList(){
        $videosModel=M('prize'); //实例化模型对象 user 表名
		$videos=$videosModel->order('prize_id desc')->select(); //查询
		$this->videos=$videos; //volist name
		// dump ($users); 
		$this->display();
}
 public function delprize(){
        // dump($_GET);
        $videoModel=M('prize');
        $vid=$_GET['vid'];
        var_dump($vid);
        $res=$videoModel->where("prize_id=$vid")->find();
        //var_dump($res);
        $photoPath=realpath("./Public/Uploads/prizephoto".$res['photo']);
        unlink($photoPath);
        if ($videoModel->where("prize_id=$vid")->delete()) {
        	$this->success('删除成功');
        }else{
        	$this->error('删除失败');
        }

    }

public function addprize(){
        if ($_FILES['files']['error']==0) {
                $loginfo=$this->MyUpload();
                $pname=$loginfo['file']['savename'];
            }
            else{
                $pname="defalt.jpg";
            }
        $videos = D('prize'); 
          $videos->create();
            $data['prize_name'] = I('post.activityname');
            $data['prize_spend'] = I('post.activityintro');
            $data['prize_amount'] = I('post.activitytel');
            $data['prize_intro'] = I('post.activitytime');
            $data['prize_date']=date('Y-m-d H:i:s');
            $data['prize_picture']=$pname;
            
            if (false !== $videos->add($data)){
                /* echo $videos->getLastSql();*/
                 // exit();
                $this->success('添加成功！', 'prizeList');
            } else {         $this->error('数据错误');           }
           
    }
 public function prizeedit(){
       if ($_FILES['files']['error']==0) {
                $loginfo=$this->MyUpload();
                $pname=$loginfo['file']['savename']; 
            }
            else{
                $pname="defalt.jpg";
            }
        $videos = D('prize'); 
        $vid=I('post.id');
           $data['prize_name'] = I('post.activityname');
            $data['prize_spend'] = I('post.activityintro');
            $data['prize_amount'] = I('post.activitytel');
            $data['prize_intro'] = I('post.activitytime');
            $data['prize_date']=date('Y-m-d H:i:s');
            $data['prize_picture']=$pname;
        dump($data);
        if(false !== $videos->where("prize_id=$vid")->save($data)){
            $this->success('成功','prizeList');
         }else{
            $this->error('失败');
         }

        }//register end
    public function editprize(){    
            $videos = M('prize'); 
            $vid=$_GET['id'];
            $res=$videos->where("prize_id=$vid")->find();
            $this->videos=$res;
         
        $this->display();
        }

    public function MyUpload(){
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath  =     './Public/Uploads/prizephoto/'; // 设置附件上传根目录
    $upload->autoSub   = false;//关闭子目录，默认ture
    //$info=$upload->uploadOne($_FILES['file']);  //上传文件

    //$upload->savePath  =     ''; // 设置附件上传（子）目录
    $upload->savename = 'time';

    $info   =   $upload->upload();  // 上传文件 
    //$dump(info);
    if(!$info) {        $this->error($upload->getError());    }
    else{return $info;}
    
    // else{        $this->success('上传成功！');    }  


    } //MyUpload end

}